package itsm.isperp.module.controller.db;

import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.domain.IBean;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.db.DbEntity;
import itsm.isperp.module.entity.db.DbEntityField;
import itsm.isperp.module.entity.db.DbEntityFieldControl;
import itsm.isperp.module.service.db.DbEntityFieldService;
import itsm.isperp.module.service.db.DbEntityService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 实体字段控制器
 * 
 * @author lizx
 * @date 2013-5-5
 */
@Controller
@RequestMapping("db/entityField")
public class DbEntityFieldController extends BaseController<DbEntityField> {

	@Autowired
	DbEntityService dbEntityService;

	@Autowired
	DbEntityFieldService dbEntityFieldService;

	@Override
	public DbEntityFieldService getService() {
		return dbEntityFieldService;
	}

	@Override
	public String getPageTitle() {
		return "实体字段管理";
	}

	/**
	 * 保存实体
	 * 
	 * @param model
	 *            　一个继承与IBean接口的尸体　{@link IBean}
	 * @param req
	 *            {@link HttpServletRequest} http请求
	 * @return json结果数据
	 */
	@Override
	@RequestMapping("save")
	@ResponseBody
	public ResultMessage save(DbEntityField model, HttpServletRequest req) {

		DbEntityFieldControl control = new DbEntityFieldControl();
		control.setControlType(req.getParameter("controlControlType"));
		control.setDatasourceEntityId(req
				.getParameter("controlDatasourceEntityId"));

		control.setDisplayField(req.getParameter("controlDisplayField"));
		control.setFilter(req.getParameter("controlFilter"));
		control.setValueField(req.getParameter("controlValueField"));
		control.setContentValue(req.getParameter("contentValue"));

		control.setId(req.getParameter("controlId"));
		// String s = req.getParameter("cbFieldControl");
		Boolean cbFieldControl = "1".equals(req.getParameter("cbFieldControl"));
		this.getService().saveOrUpdate(model, control, cbFieldControl);

		return ResultMessage.newInstance(true, null, model.getId());
	}

	/**
	 * 编辑界面
	 * 
	 * @param id
	 *            唯一标识
	 * @return 编辑视图
	 */
	@Override
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req) {

		DbEntity entity = null;
		ModelAndView mv = new ModelAndView(this.getModulePath() + "/edit");
		DbEntityField model = null;

		DbEntityFieldControl fieldControl = null;

		List<DbEntity> datasourceList = this.dbEntityService.findAll(
				new JqGridRequest().isPageable(false)).getContent();
		String entityId = null;
		if (StringUtils.isEmpty(id)) {
			entityId = req.getParameter("entityId");
			if (StringUtils.isEmpty(entityId)) {
				return null;
			}

			entity = dbEntityService.findOne(entityId);
			model = new DbEntityField();
			model.setDbEntityId(entityId);

		} else {

			fieldControl = dbEntityFieldService.findFieldControl(id);
			model = dbEntityFieldService.findOne(id);
			entity = dbEntityService.findOne(model.getDbEntityId());
			entityId = entity.getId();
		}

		if (fieldControl == null) {
			fieldControl = new DbEntityFieldControl();
			fieldControl.setId(HibernateUUIDGenerator.generate());
			fieldControl.setDbEntityFieldId(entityId);

			mv.addObject("haveControl", "");
		} else {
			mv.addObject("haveControl", "checked='checked'");
		}

		mv.addObject("pageTitle", this.getPageTitle());

		mv.addObject("model", model);

		mv.addObject("datasourceList", datasourceList);

		mv.addObject("fieldControl", fieldControl);

		return mv;
	}

	@RequestMapping("listByTableName")
	@ResponseBody
	public List<Map<String, Object>> listByTableName(String tableName) {
		JqGridRequest request = new JqGridRequest();
		request.isPageable(false);
		request.setSelectColumns("name", "column_name");
		request.addFilterRule("table_name", JqGridFilterRuleOp.eq, tableName);
		return this.getService().findAllDetailByRequest(request);

	}

	@Override
	@RequestMapping("list")
	@ResponseBody
	public PageResponse<DbEntityField> list(JqGridRequest request,
			HttpServletRequest req) {
		String entityId = req.getParameter("entityId");
		if (StringUtils.isNotEmpty(entityId)) {
			request.addFilterRule("db_entity_id", JqGridFilterRuleOp.eq,
					entityId);
			return this.getService().findAll(request.isPageable(false))
					.toPage();

		}
		return null;

	}

}
